/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mrp.productionPlanning.session;

import java.util.List;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import mrp.productionPlanning.entity.ForecastEntity;

/**
 *
 * @author GKnight
 */
@Stateless
@LocalBean
public class ForecastSessionBean {

    @PersistenceContext
    private EntityManager em;
    
    public ForecastEntity createForecast(String monthName, int yearName){
        ForecastEntity forecast = new ForecastEntity();
        forecast.setMonthName(monthName);
        forecast.setYearName(yearName);
        em.persist(forecast);
        return forecast;
    }
    
    public ForecastEntity getForecastEntityByMonth(String monthName,int yearName){
        Query query = em.createQuery("SELECT f FROM ForecastEntity f WHERE f.monthName = :monthName AND f.yearName = :yearName");
        query.setParameter("monthName", monthName);
        query.setParameter("yearName", yearName);
        ForecastEntity fc = new ForecastEntity();
        try{
            fc = (ForecastEntity)query.getSingleResult();
        }
        catch(NoResultException ex){
            System.out.println("No Result");
        }
        return fc;
    }
    
    public List<ForecastEntity> getAllForecasts(){
        Query query = em.createQuery("SELECT f FROM ForecastEntity f");
        List<ForecastEntity> historicalDatas = query.getResultList();
        return historicalDatas;
    }
    
}
